package com.yupi.springbootinit.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBCUtils {
    
    private static String url = "jdbc:mysql://localhost:3306/bi";
    private static String user = "root";
    private static String password = "123456";
    
    /**
     * 多行SQL语句批量运行
     * @param sql
     */
    public static void batchSql(String sql){
        try {
            // 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 获取数据库连接
            Connection connection = DriverManager.getConnection(url, user, password);
            // 创建Statement对象
            Statement statement = connection.createStatement();
            //切分sql语句
            String[] split = sql.split("\n");
            for (String s : split) {
                statement.addBatch(s);
            }
            // 执行SQL语句
            statement.executeBatch();
            // 关闭资源
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
